// 后台的菜单组织
window.menus = [
    {
        name: "系统配置",
        icon: "gear",
        left: [
            {
                name: "系统参数设定",
                page: "setting",
                rights: [
                    {
                        uri: "/client/title",
                        name: "网站名称",
                    },
                    {
                        uri: "/client/log_big",
                        name: "站点大LOGO",
                        cp: true,
                    },
                    {
                        uri: "/client/maintain",
                        name: "站点关闭"
                    },
                    {
                        uri: "/client/bet_enable",
                        name: "允许下注"
                    },
                    {
                        uri: "/client/charge_enable",
                        name: "允许充值"
                    },
                    {
                        uri: "/client/new_user",
                        name: "新会员注册赠送金"
                    },
                    {
                        uri: "/client/dw",
                        name: "打水倍数"
                    },
                ]
            },
            {
                name: "微信登录接口",
                page: "wx_setting",
                wx: true,
                rights: [
                    {
                        uri: "/weixin/save,/weixin/query",
                        name: "保存微信设置"
                    }
                ]
            },
            {
                name: "网站公告",
                page: "board",
                cp: true,
                rights: [
                    {
                        uri: "/client/board",
                        name: "网站公告",
                    }
                ]
            },
            {
                name: "客服代码",
                page: 'kf',
                cp: true,
                rights: [
                    {
                        uri: "/client/kf",
                        name: "客服代码",
                    }
                ]
            },
            {
                name: "首页幻灯片",
                page: 'ad',
                cp: true,
                rights: [
                    {
                        uri: "/client/ads",
                        name: "首页幻灯片",
                    }
                ],
            },
            {
                name: "幻灯片(手机)",
                page: 'ad_phone',
                cp: true,
                rights: [
                    {
                        uri: "/client/ad_phone",
                        name: "幻灯片(手机)",
                    }
                ],
            },
            {
                name: "消息弹窗",
                page: "message",
                cp: true,
                rights: [
                    {
                        uri: "/notice/query",
                        name: "检索",
                    },
                    {
                        uri: "/notice/save",
                        name: "添加",
                    },
                    {
                        uri: "/notice/delete",
                        name: "删除",
                    },
                ],
            },
            {
                name: "角色管理",
                page: "role_manager",
                rights: [
                    {
                        uri: "/client/role",
                        name: "角色管理",
                    }
                ],
            },
            {
                name: "权限管理",
                page: "rights_manager",
                rights: [
                    {
                        uri: "/user/crosier/saves,/user/crosier/deletes",
                        name: "修改",
                    },
                ],
            },
            {
                name: "后台子账户",
                page: "admin_account",
                rights: [
                    {
                        uri: "/user/create,/user/grade",
                        name: "管理员账号",
                    }
                ]
            },
        ]
    },
    {
        name: "用户管理",
        icon: "male",
        left: [
            {
                name: "会员",
                page: "huiyuan",
                rights: [
                    {
                        uri: "/account/balance",
                        name: "会员查看",
                    },
                    {
                        uri: "/client/user/modify",
                        name: "会员编辑",
                    },
                    {
                        uri: "/user/reset-pass",
                        name: "重置密码",
                    },
                    {
                        uri: "/user/reset-pass2",
                        name: "重置取款密码",
                    },
                    {
                        uri: "/user/online/sign-out",
                        name: "踢下线",
                    },
                ],
                beforeLoad: function() {
                    context.center.layerOper = undefined;
                    context.center.refereeOper = undefined;
                }
            },
            {
                name: "在线会员",
                page: "online",
                rights: "用户管理/会员",
            },
            {
                name: "分层管理",
                page: "layer",
                rights: [
                    {
                        uri: "/client/layer",
                        name: "分层管理",
                    }
                ],
            },
            {
                name: "代理",
                page: "daili",
                cp: true,
                rights: [
                    {
                        uri: "/client/daili",
                        name: "代理",
                    }
                ]
            },
            {
                name: "IP黑名单",
                page: "ip",
                cp: true,
                rights: [
                    {
                        uri: "/blacklist/save",
                        name: "IP黑名单",
                    }
                ]
            },
        ],
    },
    {
        name: "财务管理",
        icon: "money",
        left: [
            {
                name: "抽佣比例",
                page: "fee",
                wx: true,
                rights: [
                    {
                        uri: "/client/fee",
                        name: "抽佣比例",
                    },
                ]
            },
            {
                name: "抽水记录",
                page: "fee_log",
                wx: true,
                rights: [
                    {
                        uri: "/client/fee_log",
                        name: "抽水比例",
                    },
                ]
            },
            {
                name: "打水编辑",
                page: 'dw',
                cp: true,
                rights: [
                    {
                        uri: "/account/dw",
                        name: "打水编辑",
                    }
                ]
            },
            {
                name: "打水记录",
                page: 'dw_log',
                cp: true,
                rights: [
                    {
                        uri: "/account/dw/query",
                        name: "打水记录",
                    }
                ]
            },
            {
                name: "余额变更",
                page: "balance_modify",
                rights: [
                    {
                        uri: "/client/deposit",
                        name: "人工充值",
                    },
                    {
                        uri: "/client/bonus",
                        name: "奖励发放",
                    },
                    {
                        uri: "/account/penalty",
                        name: "惩罚扣款",
                    },
                    {
                        uri: "/account/deduct",
                        name: "其他扣款",
                    },
                ]
            },
            {
                name: "线下充值",
                page: "manual_charge_log",
                rights: "财务管理/账变记录",
            },
            {
                name: "线上充值",
                page: "charge_log",
                cp: true,
                rights: "财务管理/账变记录",
            },
            {
                name: "提款记录",
                page: "take_log",
                rights: "财务管理/账变记录",
            },
            {
                name: "扣款记录",
                page: "penalty_log",
                rights: "财务管理/账变记录",
            },
            {
                name: "账变记录",
                page: "balance_log",
                beforeLoad: function() {
                    context.center.balanceOper = undefined;
                },
                rights: [
                    {
                        uri: "/account/log/query",
                        name: "查看账变记录",
                    },
                    {
                        uri: "/account/log/pass,/account/log/reject,/payment/success",
                        name: "审核"
                    }
                ]
            },
        ],
    },
    {
        name: "报表管理",
        icon: "line-chart",
        left: [
            {
                name: "盈亏详情",
                page: "win_lose_total",
                cp: true,
                rights: [
                    {
                        uri: "/bet/sum/dquery,/stat/sum/dquery,/stat/sum/restat",
                        name: "盈亏详情",
                    }
                ]
            },
            {
                name: "盈亏总览",
                page: "win_lose_month_total",
                cp: true,
                rights: [
                    {
                        uri: "/bet/sum/mquery,/stat/sum/mquery,/stat/sum/restat",
                        name: "盈亏总览",
                    }
                ]
            },
            {
                name: "彩种盈亏",
                page: "win_lose_detail",
                cp: true,
                rights: [
                    {
                        uri: '/client/win_lose_detail,/stat/sum/restat',
                        name: "彩种盈亏"
                    }
                ],
            },
            {
                name: "代理报表",
                page: "daili_baobiao",
                cp: true,
                rights: [
                    {
                        uri: '/stat/user/sreferee',
                        name: '代理报表'
                    }
                ],
            },
            {
                name: "会员报表",
                page: "huiyuan_baobiao",
                cp: true,
                rights: [
                    {
                        uri: '/stat/user/squery',
                        name: '会员报表'
                    }
                ],
            },
            {
                name: "抽水统计",
                page: "group_fee",
                wx: true,
            },
        ]
    },
    {
        name: "投注记录",
        icon: "list",
        left: [
            {
                name: "今日注单",
                page: "today",
                cp: true,
                rights: "投注记录/历史注单"
            },
            {
                name: "历史注单",
                page: "bet_history",
                cp: true,
                rights: [
                    {
                        uri: "/bet/query",
                        name: "投注记录"
                    }
                ]
            }
        ]
    },
    {
        name: "游戏管理",
        icon: "gamepad",
        left: [
            {
                name: "游戏设定",
                page: "game_setting",
                rights: [
                    {
                        uri: "/client/game_setting",
                        name: "游戏设定",
                    }
                ],
            },
            {
                name: "盘口设定",
                page: "lottery_setting",
                rights: [
                    {
                        uri: "/lottery/rate/saves,/lottery/rate/backs,/lottery/rate/reset",
                        name: "盘口设定",
                    }
                ],
                beforeLoad: function() {
                    context.center.lotteryOper = undefined;
                }
            },
            {
                name: "种类限制",
                page: "number_limit",
                rights: [
                    {
                        uri: "/lottery/number/limit",
                        name: "投注种类限制",
                    }
                ]
            },
            {
                name: "六合彩",
                page: "lhc_setting",
                rights: [
                    {
                        uri: "/client/lhc",
                        name: "六合彩",
                    }
                ],
            },
        ]
    },
    {
        name: "历史开奖",
        icon: "history",
        left: [
            {
                name: "北京PK10",
                page: "result/result_bjpk10",
            },
            {
                name: "极速赛车",
                page: "result/result_zypk10",
            },
            {
                name: "重庆幸运农场",
                page: "result/result_cqkl10",
            },
            {
                name: "广东快乐十分",
                page: "result/result_gdkl10",
            },
            {
                name: "极速快乐十分",
                page: "result/result_zykl10",
            },
            {
                name: "重庆时时彩",
                page: "result/result_cqssc",
            },
            {
                name: "新疆时时彩",
                page: "result/result_xjssc",
            },
            {
                name: "天津时时彩",
                page: "result/result_tjssc",
            },
            {
                name: "极速时时彩",
                page: "result/result_zyssc",
            },
            {
                name: "腾讯分分彩",
                page: "result/result_ffcqq",
            },
            {
                name: "广东11选5",
                page: "result/result_gd115",
            },
            {
                name: "幸运11选5",
                page: "result/result_zy115",
            },
            {
                name: "北京快乐8",
                page: "result/result_bjkl8",
            },
            {
                name: "幸运快乐8",
                page: "result/result_zykl8",
            },
            {
                name: "江苏骰宝",
                page: "result/result_jsk3",
            },
            {
                name: "幸运骰宝",
                page: "result/result_zyk3",
            },
            {
                name: "香港六合彩",
                page: "result/result_lhc",
            },
        ],
        rights: [
            {
                uri: "/bet/reopen",
                name: "重新开奖",
            },
            {
                uri: "/bet/cancel",
                name: "撤单",
            },
        ],
    },
    {
        name: "充值配置",
        icon: "cc-visa",
        rights: [
            {
                uri: "/client/charge_query,/pay/query,",
                name: "查看",
            },
            {
                uri: "/client/charge_save,/pay/save,/pay/delete",
                name: "修改",
            },
        ],
        left: [
            {
                name: "微信支付配置",
                page: "wechat",
            },
            {
                name: "QQ钱包配置",
                page: "caifutong",
            },
            {
                name: "支付宝配置",
                page: "alipay",
            },
            {
                name: "银行支付配置",
                page: "bank",
            },
            {
                name: "在线支付配置",
                page: "3rdpay",
                cp: true,
            },/*
            {
                name: "充值方式",
                page: "pay_channel"
            }*/
        ]
    },
    {
        name: '游戏开奖',
        icon: "angellist",
        rights: [
            {
                uri: "/lottery/zy/save,/lottery/zy/query",
                name: "设置开奖结果"
            },
            {
                uri: "/lottery/save",
                name: "修改开奖结果",
            }
        ],
        left: [
            {
                name: "北京PK10",
                page: "manual/bjpk10_manual_result",
            },
            {
                name: "极速赛车",
                page: "manual/zypk10_manual_result",
            },
            {
                name: "重庆幸运农场",
                page: "manual/cqkl10_manual_result",
            },
            {
                name: "广东快乐十分",
                page: "manual/gdkl10_manual_result",
            },
            {
                name: "极速快乐十分",
                page: "manual/zykl10_manual_result",
            },
            {
                name: "重庆时时彩",
                page: "manual/cqssc_manual_result",
            },
            {
                name: "新疆时时彩",
                page: "manual/xjssc_manual_result",
            },
            {
                name: "天津时时彩",
                page: "manual/tjssc_manual_result",
            },
            {
                name: "极速时时彩",
                page: "manual/zyssc_manual_result",
            },
            {
                name: "腾讯分分彩",
                page: "manual/ffcqq_manual_result",
            },
            {
                name: "广东11选5",
                page: "manual/gd115_manual_result",
            },
            {
                name: "幸运11选5",
                page: "manual/zy115_manual_result",
            },
            {
                name: "北京快乐8",
                page: "manual/bjkl8_manual_result",
            },
            {
                name: "幸运快乐8",
                page: "manual/zykl8_manual_result",
            },
            {
                name: "江苏骰宝",
                page: "manual/jsk3_manual_result",
            },
            {
                name: "幸运11选3",
                page: "manual/zyk3_manual_result",
            },
            {
                name: "香港六合彩",
                page: "manual/lhc_manual_result",
            },
        ],
    },
    {
        name: "群组管理",
        icon: "gg",
        wx: true,
        left: [
            {
                name: "群上限",
                page: "group_limit"
            },
            {
                name: "群组查询",
                page: "group_list"
            },
        ]
    },
    {
        name: "活动管理",
        icon: "trophy",
        left: [

        ]
    },
    {
        name: "代理结算",
        icon: "usd",
        left: [
            {
                name: "代理结算报表",
                page: "daili_jiesuan_baobiao",
                cp: true,
                rights: [
                    {
                        uri: '/stat/referee/query',
                        name: '代理结算报表'
                    }
                ]
            },
            {
                name: "代理结算审核",
                page: "daili_shenhe",
                cp: true,
                rights: [
                    {
                        uri: '/stat/referee/pass,/stat/referee/reject'
                    }
                ]
            },
            {
                name: "代理结算配置",
                page: "daili_settings",
                cp: true,
                rights: [
                    {
                        uri: '/client/referee',
                        name: '代理结算配置'
                    }
                ],
                beforeLoad: function() {
                    context.center.refereeBonus = undefined;
                }
            }
        ]
    },
    {
        name: "平台费用",
        icon: "cubes",
        left: [
            {
                name: "平台费用结算",
                cp: true,
                page: ""
            },
            {
                name: "付款记录",
                cp: true,
                page: ""
            }
        ]
    },
    {
        name: "日志管理",
        icon: "file-archive-o",
        left: [
            {
                name: "登录日志",
                page: "login_log",
            },
            {
                name: "登录失败日志",
                page: "login_fail_log",
            },
            {
                name: "操作日志",
                page: "oper_log",
                rights: [
                    {
                        uri: "/journal/query",
                        name: "操作日志",
                    }
                ]
            },
            {
                name: "异常日志",
                page: "special_log",
            },
        ]
    }
];

// 只显示权限范围内的菜单
function checkSubMenu(m, m0) {
    var grade = context.user.user.grade || '1';
    if (m0 === '游戏开奖') {
        if (Rights.check("/lottery/zy/save", grade) && 
            (m.name.startsWith("极速") || m.name.startsWith("幸运"))) return true;
        if (Rights.check("/lottery/save", grade) && 
            (!m.name.startsWith("极速") && !m.name.startsWith("幸运"))) return true;
        return false;
    }

    var rights = m.rights;
    if (typeof(rights) == 'string') {
        var arr = rights.split("/");
        return checkSubMenu(findMenu2(findMenu1(arr[0]), arr[1]), m0);
    }
    if (Util.isEmpty(rights)) return true;
    for (var i in rights) {
        var right = rights[i].uri;
        var arr = right.split(",");
        for (var j in arr) {
            // 只要具备其中一个权限，就显示出来
            if (Rights.check(arr[j], grade) == 1) return true;
        }
    }
    return false;
}
function checkMenu(m) {
    var grade = context.user.user.grade || '1';
    var rights = m.rights;
    if (Util.isEmpty(rights)) return true;
    for (var i in rights) {
        var right = rights[i].uri;
        var arr = right.split(",");
        for (var j in arr) {
            // 只要具备其中一个权限，就显示出来
            if (Rights.check(arr[j], grade) == 1) return true;
        }
    }
    return false;
}
function filterMenuByRight() {
    var menus2 = {};
    for (var k in menus) {
        var m = menus[k];
        if (!checkMenu(m)) continue;
        for (var k2 in m.left) {
            if (!checkSubMenu(m.left[k2], m.name)) continue;

            // 需要显示
            if (!menus2[k]) menus2[k] = {
                name: m.name,
                icon: m.icon,
                wx: m.wx,
                cp: m.cp,
                left: [],
            };
            menus2[k].left.push(m.left[k2]);
        }
    }
    return menus2;
}

ME.query(function() {
    Rights.init(function() {
        Layer._update(function() {
            afterGotRights();
        });
    });
});

function findMenu1(n) {
    for (var i in menus) {
        if (menus[i].name == n) return menus[i];
    }
}
function findMenu2(menu, n) {
    for (var i in menu.left) {
        if (menu.left[i].name == n) return menu.left[i];
    }
}

var afterGotRights = function() {
    // 填充左侧菜单
    var ul = $(".left .left_menus");
    ul.empty();
    var menus2 = filterMenuByRight();
    for (var i in menus2) {
        if (window.bocai && menus2[i].cp == true) continue;
        if (!window.bocai && menus2[i].wx == true) continue;

        var li = $('<div class="item" value="' + menus2[i].name + '"></div>');
        li.append('<a href="#" class="parent"><span class="am-icon-' + menus2[i].icon + ' am-icon-sm"> ' + menus2[i].name + '</span></a>');

        var submenus = $('<div class="submenus"></div>');
        for (var j in menus2[i].left) {
            if (window.bocai && menus2[i].left[j].cp == true) continue;
            if (!window.bocai && menus2[i].left[j].wx == true) continue;

            var li2 = $('<a title="提示：按住Ctrl键点击，可新窗口全屏打开页面" href="#" class="" value="' + menus2[i].name + '">' + menus2[i].left[j].name + '</a>');
            submenus.append(li2);
        }

        li.append(submenus);
        ul.append(li);
    }

    // 主菜单点击的处理
    $(".left .item .parent").click(function(e) {
        var p = $(e.currentTarget).parent();
        if (p.attr("class").indexOf("active") != -1) {
            // 菜单缩起来
            p.attr("class", "item");
        }
        else {
            $(".left .item").attr("class", "item");
            p.attr("class", "item active");
        }
    });

    // 设置菜单选中
    var n1 = urlInfo.params.n1;
    if (n1) {
        var n2 = urlInfo.params.n2;
        if (!n2 && n1) n2 = findMenu1(n1).left[0].name;
        n1 = decodeURI(n1);
        n2 = decodeURI(n2);
    }
    
    // 菜单被点击的处理
    $(".left .submenus a").click(function(e) {
        var t = $(e.currentTarget);
        var n1 = t.attr("value"),
            n2 = t.html();

        if (!e.ctrlKey) {
            var f = findMenu2(findMenu1(n1), n2).beforeLoad;
            if (f) f();
            PageHistory.load(n1, n2, 0);
        }
        else {
            var url = Util.genUrl({
                n1: n1,
                n2: n2,
                full_screen: 1,
                restore: 0,
            });
            window.open(url);
        }
    });

    // 填充顶部菜单
    var ul = $("#top-menu");
    ul.empty();
    for (var i in menus) {
        ul.append("<a href='#'>" + menus[i].name + "</a>");
    }
    var arr = $("#top-menu a");
    for (var i = 0; i < arr.length; i++) {
        $(arr[i]).click(function(e) {
            var n = $(e.currentTarget).html();
            var n1 = urlInfo.params.n1 || menus[0].name;
            n1 = decodeURI(n1);
            if (n == n1) return;

            Util.jumpUrl({
                n1: n,
                n2: "",
            });
        });
    }

    // 载入html文件
    if (n1 && n2) {
        PageHistory.load(n1, n2, 0);

        // 设置导航路径
        var ol = $(".am-breadcrumb");
        ol.empty();
        ol.append('<li><a href="#" class="am-icon-home">首页</a></li>');
        ol.append('<li><a href="#">' + n1 + '</a></li>');
        ol.append('<li><a href="#" class="am-active">' + n2 + '</a></li>');
    }
    else {
        n1 = "index"; n2 = "index";
        PageHistory.load(n1, n2, 0);

        var ol = $(".am-breadcrumb");
        ol.empty();
        ol.append('<li><a href="#" class="am-icon-home">首页</a></li>');
        ol.append('<li><a href="#" class="am-active">默认</a></li>');
    }
    
    BalanceClueUI.init();
    TailClue.init();
};
